//
//  DetailViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.titleStr;
    
    self.navigationController.navigationBar.translucent = NO;
    
    // 设置标题栏右侧的按钮(用于收藏与分享)
    UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect.png"];
    
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBarButton.backgroundColor = [UIColor clearColor];
    rightBarButton.frame = CGRectMake(0, 0, 25, 25);
    [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
    [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
    
    // 标记为未收藏
    self.collect = NO;
    
    
    // 打开数据库
    [[ClassifyDataBase sharkClassifyDataBase] openDB];
    // 查询数据库中的信息
    for (DetailModel *detailModel in [[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail]) {
        
        // 判断当前收藏的菜谱是否已经存在数据库中
        // 如果数据库中存在该菜谱, 显示已经收藏
        if ([detailModel.Title isEqualToString:self.titleStr]) {
            // 修改标题栏右侧的按钮
            UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect2.png"];
            
            UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightBarButton.backgroundColor = [UIColor clearColor];
            rightBarButton.frame = CGRectMake(0, 0, 25, 25);
            [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
            [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
            
            // 标记为已经收藏
            self.collect = YES;
        }
    }
    
    
    
    // 菊花类
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    
    self.HUD.labelText = @"加载中";
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
        [self doTask];
    } completionBlock:^{
        
        
    }];
    
    // 利用封装的AFN解析
    // 初始化数组
    //    self.allDetailArr = [NSMutableArray array];
    
    NSString *url_Str = [NSString stringWithFormat:@"http://api.hoto.cn/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1406271508167&vc=57&vn=3.7.0&loguid=0&deviceid=haodou864052025053998&uuid=c4affd54-af74-4a3d-8058-d434982df3c3&channel=360_v370&method=Info.getinfoV3&virtual=&uid=0&&rid=%@", self.rid];
    [GetAFN downLoadBlock:url_Str block:^(id result) {
        
        NSMutableDictionary *allDetailDic = result;
        NSMutableDictionary *resultDic = [allDetailDic objectForKey:@"result"];
        NSMutableDictionary *infoDic = [resultDic objectForKey:@"info"];
        self.detaiModel = [[DetailModel alloc] init];
        [self.detaiModel setValuesForKeysWithDictionary:infoDic];
        
        // 加载完成,取消菊花
        [self.HUD removeFromSuperview];
        
        // 初始化detailScorllView, 并为其传值
        self.detailScorllView = [[DetailScorllView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) detail:self.detaiModel];
        
        [self.view addSubview:self.detailScorllView];
        
    }];
    
}
// 菊花类自定义方法(最长显示时间20秒)
- (void)doTask
{
    sleep(20);
}
// 实现右侧按钮的功能
- (void)rightBarButtonAction:(UIBarButtonItem *)button
{
    if (self.collect == YES) {
        // 弹出提示框
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享", @"取消收藏", nil];
        [actionSheet showInView:self.view];
    }else{
        // 弹出提示框
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"分享", @"收藏", nil];
        [actionSheet showInView:self.view];
    }
    
}
// 点击ActionSheet的方法 (签协议完成)
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"00000");
    }else if (buttonIndex == 1) {
        
        
        // 打开数据库
        [[ClassifyDataBase sharkClassifyDataBase] openDB];
        // 创建数据库表
        [[ClassifyDataBase sharkClassifyDataBase] createTable];
        
        // 查询数据库中的信息
        for (DetailModel *detailModel in [[ClassifyDataBase sharkClassifyDataBase] selectClassifyDetail]) {
            
            // 判断当前收藏的菜谱是否已经存在数据库中
            if ([detailModel.Title isEqualToString:self.detaiModel.Title]) {
                
                [[ClassifyDataBase sharkClassifyDataBase] deleteClassify:detailModel.Title];
                
                // 修改标题栏右侧的按钮
                UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect.png"];
                
                UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
                rightBarButton.backgroundColor = [UIColor clearColor];
                rightBarButton.frame = CGRectMake(0, 0, 25, 25);
                [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
                [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
                
                
                [self showAlert:@"取消收藏!"];
                
                // 修改ActionSheet的点击标记
                self.collect = NO;
                
                // 已经被收藏, 就直接跳出
                return;
            }
        }
        {
            // 插入数据
            [[ClassifyDataBase sharkClassifyDataBase] insertClassifyDetail:self.detaiModel];
            
            // 修改标题栏右侧的按钮
            UIImage *rightBarButtonImage = [UIImage imageNamed:@"collect2.png"];
            
            UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightBarButton.backgroundColor = [UIColor clearColor];
            rightBarButton.frame = CGRectMake(0, 0, 25, 25);
            [rightBarButton setImage:rightBarButtonImage forState:UIControlStateNormal];
            [rightBarButton addTarget:self action:@selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
            
            [self showAlert:@"收藏成功!"];
            
            // 修改ActionSheet的点击标记
            self.collect = YES;
        }
        
    }
    
}
// 设置弹出提示框, 自动消失的方法
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}

- (void)showAlert:(NSString *) message // 时间
{
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    // 定时器
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:promptAlert repeats:YES];
    [promptAlert show];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
